﻿using System;
using System.Collections;
using System.Data;
using System.Text;
using Col = Column<string, System.Type, int>;

/// <summary>
/// Summary description for DummyData
/// </summary>
public static class DummyData
{
    #region Define Data Type

    public static Type IntegerType { get { return Type.GetType("System.Int32"); } }
    public static Type LongType { get { return Type.GetType("System.Int64"); } }
    public static Type ShortType { get { return Type.GetType("System.Int16"); } }
    public static Type SingleType { get { return Type.GetType("System.Single"); } }
    public static Type DoubleType { get { return Type.GetType("System.Double"); } }
    public static Type DecimalType { get { return Type.GetType("System.Decimal"); } }
    public static Type DateTimeType { get { return Type.GetType("System.DateTime"); } }
    public static Type StringType { get { return Type.GetType("System.String"); } }
    public static Type BooleanType { get { return Type.GetType("System.Boolean"); } }

    #endregion

    #region Random Data
    /// <summary>
    /// Generates a random string with the given length
    /// </summary>
    /// <param name="size">Size of the string</param>
    /// <returns>Random string</returns>
    private static string RandomString(int len)
    {
        StringBuilder builder = new StringBuilder();
        Random random = new Random();
        char ch;
        for (int i = 0; i < len; i++)
        {
            ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            builder.Append(ch);
        }
        return builder.ToString();
    }

    private static int RandomNumber(int min, int max)
    {
        Random random = new Random();
        return random.Next(min, max);
    }

    private static decimal RandomDecimal(int min, int max)
    {
        return Convert.ToDecimal(RandomNumber(min, max));
    }

    private static DateTime RandomDateTime()
    {
        int value = RandomNumber(1, 10);
        return DateTime.Now.AddDays(value).AddMonths(value);
    }


    private static bool RandomBoolean()
    {
        int value = RandomNumber(0, 1);
        return value == 1 ? true : false;
    }

    #endregion


    public static DataTable GetDummyData(ArrayList columnList, int totalRows)
    {
        DataTable list = new DataTable();
        //Step1: Add columns
        for (int i = 0; i < columnList.Count; i++)
        {
            Col item = (Col)columnList[i];
            list.Columns.Add(new DataColumn(item.ColumnName, StringType));
        }
        //Step2: Add data into rows
        for (int index = 0; index < totalRows; index++)
        {
            DataRow row = list.NewRow();
            for (int i = 0; i < columnList.Count; i++)
            {
                Col item = (Col)columnList[i];
                int len = item.ColumnLength;
                int minValue = 0;
                int maxValue = len;

                if (item.ColumnType == IntegerType)
                {
                    int tempMax = 1;
                    for (int k = 1; k < maxValue; k++)
                        tempMax *= 10;
                    row[item.ColumnName] = RandomNumber(tempMax, tempMax * 10);
                }
                else if (item.ColumnType == StringType)
                {
                    row[item.ColumnName] = RandomString(len);
                }
                else if (item.ColumnType == DecimalType)
                {
                    row[item.ColumnName] = RandomDecimal(minValue, maxValue);
                }
                else if (item.ColumnType == DateTimeType)
                {
                    row[item.ColumnName] = RandomDateTime();
                }
                else if (item.ColumnType == BooleanType)
                {
                    row[item.ColumnName] = RandomBoolean();
                }
                else if (item.ColumnType == LongType)
                {

                }
                else if (item.ColumnType == ShortType)
                {

                }
                else if (item.ColumnType == SingleType)
                {

                }
                else if (item.ColumnType == DoubleType)
                {

                }
            }
            list.Rows.Add(row);
        }



        return list;
    }
}

public class Column<Name, Type, Length>
{
    public Name ColumnName { get; set; }
    public Type ColumnType { get; set; }
    public Length ColumnLength { get; set; }
    public Column(Name columnName, Type dataType, Length maxLength)
    {
        this.ColumnName = columnName;
        this.ColumnType = dataType;
        this.ColumnLength = maxLength;
    }
}
